import HModal from "@/components/ant-ext/HModal";
import {useTableActionRedux} from "@/components/ant-ext-table/redux/HAdminContext";
import {Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import {useActionForm} from "@/common/hooks";
import {useEffect, useState} from "react";
import EntityFeeList, {EntityFeeListModel} from "@/api/entity-fee-list";


export default function FeeListEdit() {
    const [loading, setLoading] = useState(false)
    const {action, reset} = useTableActionRedux<EntityFeeListModel>()
    const {form, key, title} = useActionForm(action, [EntityFeeList.EDIT], EntityFeeList.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    useEffect(() => {
        if (action.match(EntityFeeList.EDIT)) {
            EntityFeeList.selectById({
                params: {id: action.getRow()?.id}, setLoading, success: (data) => {
                    form.setFieldsValue(data)
                }
            })
        }
    }, [action, form])


    return <>
        <HModal onCancel={close} type={"setUp"} style={{width: "400px", height: "200px"}} confirmLoading={loading}
                onOk={form.submit}
                visible={title !== undefined}
                title={title}
        >
            <Form name={"editForm"} form={form} labelCol={{sm: 4}} onFinish={() => {
                EntityFeeList.doEdit({
                    body: form.getFieldsValue(), setLoading, success: () => {
                        close()
                    }
                })
            }}>
                <HFormItem label={"id"} name={"id"} disabled={true} hidden={key === EntityFeeList.ADD}/>
                <HFormItem label={"金额"} name={"amount"} required/>
                <HFormItem label={"收费"} name={"fee"} required/>
                <HFormItem label={"返现"} name={"cashback"} required/>
            </Form>
        </HModal>
    </>
}